package com.example.demo.config;

import com.example.demo.model.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@ResponseBody
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public ReturnResult<Object> handlerException(Exception e){
        log.error("发生错误：e", e);
        return ReturnResult.error("内部错误");
    }

    @ExceptionHandler
    public ReturnResult<Object> handlerException(ArithmeticException e){
        log.error("发生错误：e", e);
        return ReturnResult.error("算术错误");
    }

    @ExceptionHandler
    public ReturnResult<Object> handlerException(NullPointerException e){
        log.error("发生错误：e", e);
        return ReturnResult.error("空指针异常");
    }

    @ExceptionHandler
    public ReturnResult<Object> handlerException(NoResourceFoundException e){
        return ReturnResult.error("资源未找到");
    }
}
